package cn.kgc.bdqn_studentinfomanager.service.StudentService.impl;

import cn.kgc.bdqn_studentinfomanager.dao.student.StudentMapper;
import cn.kgc.bdqn_studentinfomanager.dao.user.UserMapper;
import cn.kgc.bdqn_studentinfomanager.pojo.Model.BdqnStudentinfo;
import cn.kgc.bdqn_studentinfomanager.pojo.common.Const;
import cn.kgc.bdqn_studentinfomanager.pojo.common.ServerResponse;
import cn.kgc.bdqn_studentinfomanager.pojo.dto.AddStudentDto;
import cn.kgc.bdqn_studentinfomanager.pojo.vo.*;
import cn.kgc.bdqn_studentinfomanager.service.StudentService.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

/**
 * by Shanshui CC on 2020/12/7 17:37
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 招生老师界面\咨询师  进入时  加载学生信息
     *
     * @return
     */
    @Override
    public ServerResponse LoadingStudentInfoByRecruitId(String token) {
        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        int userId = Integer.parseInt(
                StringUtils.substring(token,
                        token.indexOf(":") + 1, token.indexOf("-")));

        int roleId = Integer.parseInt(
                StringUtils.substring(token,
                        token.indexOf("-") + 1, token.indexOf("-") + 2));
        System.out.println("RoleId是" + roleId);

        List<GetStudentVo> getStudentVoList = studentMapper.queryStudentInfoByconditionId(userId + "", roleId + "");

        return ServerResponse.createBySuccess(getStudentVoList);
    }


    /**
     * 专员界面 进入时  加载学生信息
     *
     * @param token
     * @return
     */
    @Override
    public ServerResponse LoadingStudentInfoByByComm(String token) {
        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        //从token中获取用户id
        int userId = Integer.parseInt(
                StringUtils.substring(token,
                        token.indexOf(":") + 1, token.indexOf("-")));

        //查专员招的学生
        List<GetStudentVo> studentVoListByComm = studentMapper.queryStudentInfoByComm(userId + "");
        //查专员下的招生老师招的学生
        List<GetStudentVo> studentVoListByCon = studentMapper.queryStudentInfoBycon(userId + "");

        studentVoListByComm.addAll(studentVoListByCon);

        return ServerResponse.createBySuccess(studentVoListByComm);
    }

    /**
     * 跳转至添加学生界面时，初始化省
     *
     * @return
     */
    @Override
    public ServerResponse loadingProvince(String token) {
        //判空
        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        //查询省份
        List<ProvinceAndCityVo> provinceAndCityVoList = studentMapper.loadingProvince();
        //判空
        if (provinceAndCityVoList == null) {
            return ServerResponse.createByErrorMessage("查询省份为空");
        }
        //返回
        return ServerResponse.createBySuccess(provinceAndCityVoList);
    }

    /**
     * 跳转至添加学生界面时，初始化市
     *
     * @return
     */
    @Override
    public ServerResponse loadingCityByProvinceId(String token, String provinceId) {
        //判空
        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        if (StringUtils.isBlank(provinceId)) {
            return ServerResponse.createByErrorMessage("省份Id为空");
        }
        //查询城市
        List<ProvinceAndCityVo> provinceAndCityVoList = studentMapper.loadingCityByProvinceId(provinceId);

        //返回
        return ServerResponse.createBySuccess(provinceAndCityVoList);

    }


    /**
     * 添加学生信息
     *
     * @param addStudentDto
     * @return
     */
    @Override
    public ServerResponse add(AddStudentDto addStudentDto, String token) {

        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        //根据手机号查询学生是否存在，如果存在则提醒
        Integer student = studentMapper.queryStudentByPhone(addStudentDto.getStuPhone());
        if (student >= 1) {
            return ServerResponse.createByErrorMessage("该手机号已存在");
        }

        //从token中获取用户id
        int userId = Integer.parseInt(
                StringUtils.substring(token,
                        token.indexOf(":") + 1, token.indexOf("-")));

        //根据用户id查询用户职位
        Integer role = studentMapper.queryTeacherInfoById(userId);
        if (role.equals(Const.UserRole.THREE)) {
            //专员录入
            addStudentDto.setStuCommissionerid(userId);
        } else if (role.equals(Const.UserRole.FOUR)) {
            //招生老师录入
            addStudentDto.setStuRecruitid(userId);
        }
        addStudentDto.setStuStatus(Const.UserRole.ONE);       //学生状态
        //在学生表插入信息
        Integer studentTable = studentMapper.insertStudentInfo(addStudentDto);
        //添加父级Id 为添加意向描述做准备
        addStudentDto.setParentId(userId);
        //在意向描述表中插入信息
        Integer descriptionTable = studentMapper.insertDescription(addStudentDto);
        //判断是否插入成功
        if (studentTable > 0 && descriptionTable > 0) {
            return ServerResponse.createBySuccessMessage("成功录入学生信息！");
        }
        return ServerResponse.createByErrorMessage("录入学生信息失败！");
    }

    /**
     * 统计学生数量 / 月
     *
     * @param token
     * @return
     */
    @Override
    public ServerResponse totalestudentsnum(String token) {

        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }

        //从token中拿到用户id
        String id = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //学生对象按月进行分类   只能查本月
        Integer integer = userMapper.queryStudentInfoCountByCreatedate(Integer.parseInt(id), null);

        return ServerResponse.createBySuccessMessage("本月共有" + integer + "名学生录入");

    }

    /**
     * 统计学生列表 /月
     *
     * @param token
     * @return
     */
    @Override
    public ServerResponse totalestudents(String token) {
        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }

        //从token中拿到用户id
        String id = org.apache.commons.lang3.StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //学生对象按月进行分类   只能查本月
        List<BdqnStudentinfo> bdqnStudentinfos =
                userMapper.queryStudentInfoListByCreatedate(
                        Integer.parseInt(id), null);

        return ServerResponse.createBySuccess(bdqnStudentinfos);
    }

    /**
     * 统计报名数量 / 月
     *
     * @return
     */
    @Override
    public ServerResponse enrolledstudentsnum(String token) {
        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }
        //从token中拿到用户id
        String id = org.apache.commons.lang3.StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //学生对象按月进行分类   只能查本月
        Integer integer = userMapper.queryStudentInfoCountByCreatedate(Integer.parseInt(id), 3);

        return ServerResponse.createBySuccessMessage("本月共有" + integer + "名学生报名");
    }

    @Override
    public ServerResponse enrolledstudents(String token) {
        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }

        //从token中拿到用户id
        String id = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //学生对象按月进行分类   只能查本月
        List<BdqnStudentinfo> bdqnStudentinfos =
                userMapper.queryStudentInfoListByCreatedate(
                        Integer.parseInt(id), 3);

        return ServerResponse.createBySuccess(bdqnStudentinfos);
    }


//    /**
//     * 修改学生信息
//     *
//     * @param bdqnStudentinfo
//     * @return
//     */
//    @Override
//    public ServerResponse updateinfo(BdqnStudentinfo bdqnStudentinfo) {
//
//        if (bdqnStudentinfo == null) {
//            return ServerResponse.createByErrorMessage("参数错误");
//        }
//
//        Integer integer = studentMapper.updateStudentInfo(bdqnStudentinfo);
//        if (integer >= 1) {
//            return ServerResponse.createBySuccessMessage("修改学生信息成功");
//        }
//        //拿学生id 当做 where 条件  这个id 应该是前端给的
//        return ServerResponse.createByErrorMessage("修改学生信息失败");
//    }

    /**
     * 点击页面时将所有学生信息加载出来
     *
     * @param token
     * @return
     */
    @Override
    public ServerResponse loadingStudentInfo(String token) {
        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }
        //从token中拿到用户id
        String parentId = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        List<GetStudentVo> getStudentVoList = studentMapper.queryStudentInfo(parentId);

        return ServerResponse.createBySuccess(getStudentVoList);

    }

    /**
     * 模糊查询学生信息
     *
     * @param condition
     * @param status
     * @return
     */
    @Override
    public ServerResponse getstudentinfo(String condition, String status, String token) {
        if (StringUtils.isBlank(condition) && StringUtils.isBlank(status)) {
            return ServerResponse.createByErrorMessage("信息不能为空");
        }
        //从token中拿到用户id
        String parentId = org.apache.commons.lang3.StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //专员旗下招生老师模糊查
        List<GetStudentVo> getStudentListByComm = studentMapper.queryStudentInfoByComm_vague(parentId, condition, status);
        //招生老师模糊查
        List<GetStudentVo> getStudentListByCon = studentMapper.queryStudentInfoBycon_vague(parentId, condition, status);

        getStudentListByComm.addAll(getStudentListByCon);

        return ServerResponse.createBySuccess(getStudentListByComm);
    }

    /**
     * 展示一条学生信息
     *
     * @param studentId
     * @return
     */
    @Override
    public ServerResponse showStudentInfoById(String token, String studentId) {
        //判空
        if (StringUtils.isBlank(token) || StringUtils.isBlank(studentId)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        //根据学生ID查询除意向描述外所有信息
        StudentinfoVo studentinfoVo = studentMapper.queryStudentInfoById(studentId);
        //根据学生Id查询所有意向描述信息
        List<Alldescription> alldescriptionList = studentMapper.queryAlldescriptionByStudentId(studentId);

        //将所有意向描述信息赋值到最后返回对象
        studentinfoVo.setAlldescription(alldescriptionList);

        return ServerResponse.createBySuccess(studentinfoVo);
    }


    /**
     * 展示一条学生信息后点击保存    (修改学生信息)
     *
     * @param addStudentDto
     * @return
     */
    @Override
    public ServerResponse saveStudentInfoById(AddStudentDto addStudentDto,String token) {

        if(StringUtils.isBlank(token)){
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        //从token中获取用户id
        int userId = Integer.parseInt(
                StringUtils.substring(token,
                        token.indexOf(":") + 1, token.indexOf("-")));
        //添加添加人Id
        addStudentDto.setParentId(userId);
        //修改学生表里

        Integer studentTable = studentMapper.updateStudentInfo(addStudentDto);
        //修改意向描述表里
        if(!StringUtils.isBlank(addStudentDto.getDescription())){
            //修改意向描述表里
            Integer descriptionTable = studentMapper.updateDescription(addStudentDto);
        }
        if (studentTable > 0) {
            return ServerResponse.createBySuccessMessage("修改学生信息成功");
        }
        return ServerResponse.createByErrorMessage("修改学生信息失败");
    }

    /**
     * 修改学生信息前预准备   (根据学生Id加载所有省份集合和当前学生所在的省份)
     *
     * @return
     */
    @Override
    public ServerResponse getProvinceListAndProvince(String token, String studentId) {
        if (StringUtils.isBlank(token) || StringUtils.isBlank(studentId)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }

        //查询当前学生的省份名称
        String province = studentMapper.getProvinceListAndProvince(studentId);
        //查询所有省份
        List<ProvinceAndCityVo> provinceAndCityVoList = studentMapper.loadingProvince();
        //最后返回的Vo
        ProvinceListAndProvinceVo provinceListAndProvinceVo = new ProvinceListAndProvinceVo();

        provinceListAndProvinceVo.setProvinceName(province);
        provinceListAndProvinceVo.setProvinceList(provinceAndCityVoList);

        return ServerResponse.createBySuccess(provinceListAndProvinceVo);
    }


    /**
     * 修改学生信息前预准备   (根据学生Id加载所有城市集合和当前学生所在的城市)
     *
     * @return
     */
    @Override
    public ServerResponse getCityListAndCity(String token, String studentId) {

        if (StringUtils.isBlank(token) || StringUtils.isBlank(studentId)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }

        //查询当前学生的城市名称
        String city = studentMapper.getCityListAndCity(studentId);
        //查询当前城市所属的省份Id  为查询所有城市做准备
        String provinceId = studentMapper.getProvinceIdByStudentId(studentId);
        //查询所有城市
        List<ProvinceAndCityVo> provinceAndCityVoList = studentMapper.loadingCityByProvinceId(provinceId);

        //最后返回的Vo
        CityListAndProvinceVo cityListAndProvinceVo = new CityListAndProvinceVo();
        cityListAndProvinceVo.setCityName(city);
        cityListAndProvinceVo.setCityList(provinceAndCityVoList);

        return ServerResponse.createBySuccess(cityListAndProvinceVo);
    }

    /**
     * 修改学生信息前预准备   (展示所有意向描述)
     *
     * @param token
     * @param studentId
     * @return
     */
    @Override
    public ServerResponse showAllDescription(String token, String studentId) {
        //判空
        if (StringUtils.isBlank(token) || StringUtils.isBlank(studentId)) {
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        List<Alldescription> allDescriptionList = studentMapper.queryAlldescriptionByStudentId(studentId);

        return ServerResponse.createBySuccess(allDescriptionList);
    }
}
